package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class PolicyPrint implements ServiceBackInvoke{
	private static final Log log = LogFactory.getLog(PolicyPrint.class);

	public String doCallBack(EliteBean elite, HttpServletRequest request,HttpServletResponse response) {
		
		String charset = "utf-8"; //设置字符集
		int timeout = 100000; //设置请求超时时间
		int enableSSLflag = 0;	
		Map headMap=new HashMap(); //设置头文件
		headMap.put("Content-Type","application/json");		
	    log.debug("----------------开始执行保单打印Grovvy----------------");
		String selectSQL = "select t1.Policy_No,t2.jq_policycode,t2.sy_policycode,t2.quote_no,t2.riskcode,SUBSTR(t1.qt_id,0,4) as qt_id  from tabcrelation t1 left join Tabccarinsurance t2 on t1.insurance_relation_id = t2.relation_id left join printedchart t3 on t2.quote_no = t3.PROPOSALNO where t1.status = '1' and (t3.compulsory_printdate is  null or t3.business_printdate is  null) and t2.full_policy_no is not null and t2.paydate>=sysdate-20";		
		def resultList = eliteBean.chaxun_sql(selectSQL, "dataSource");
		def resultListNum = resultList.size();
		//log.debug("需要打印的调用数量:" + resultListNum);
		//获取保单打印地址
		String selectSQL2="select (WEBSERVICEURL||(select WEBSERVICEURL from sysc_interface where WEBTYPE='BDDY2')) as WEBSERVICEURL from sysc_interface where WEBTYPE='BJURL'";
		def postURL2=eliteBean.chaxun_sql(selectSQL2, "dataSource");
		def postURL=postURL2[0].get("WEBSERVICEURL");
		//log.debug("打印调用地址:" + postURL);
		//log.debug("postURL值:" + postURL);
		//log.debug("本次调用数据:" + resultList);
		
		def policyNo="";
		def compulsoryPolicyNo="";
    	def businessPolicyNo="";	
		def proposalNo="";				
		def riskCode="";
		def QT_ID = "";
		//回写打印状态等信息
		def succ="";
		def ErrType="";
		def ErrMsg="";
		//双险打印时间回写
		def reCompulsoryPrintDate="";
		def reBusinessPrintDate="";
		if(resultListNum>0) 
		{	log.debug("开始调用保单打印标识接口");								
			def postXml="";
			def result="";
			
			for (int i = 0; i < resultListNum; i++) 
			{	
			    def queryMap1=resultList[i];
				//log.debug("开始执行第"+i+"行");
			    policyNo=queryMap1.get("Policy_No");
				compulsoryPolicyNo=queryMap1.get("jq_policycode");
				businessPolicyNo=queryMap1.get("sy_policycode")
				proposalNo=queryMap1.get("quote_no");
				riskCode=queryMap1.get("riskcode");
				QT_ID=queryMap1.get("qt_id");
				if(compulsoryPolicyNo==null)
				{
					compulsoryPolicyNo="";
				}
				if(businessPolicyNo==null)
				{
					businessPolicyNo="";
				}
				//log.debug("SQL查询打印得大保单信息："+ policyNo);
				//log.debug("SQL查询打印得交强险信息："+ compulsoryPolicyNo);
				//log.debug("SQL查询打印得商业险信息："+ businessPolicyNo);
				//log.debug("SQL查询打印得投保单信息："+ proposalNo);
				//log.debug("SQL查询打印得险种信息：  "+ riskCode);
				//log.debug("SQL查询打印得是否新旧单："+ QT_ID);
					//拼接双险并存报文
					postXml="{\"policyNo\":\""+policyNo+"\",\n" +"\"compulsory_policyNo\":\""+compulsoryPolicyNo+"\",\n" +"\"commercial_policyNo\":\""+businessPolicyNo+"\",\n" +"\"qt_id\":\""+QT_ID+"\"}";
					//log.debug("打印全报文:" + postXml); 

				log.debug("开始调用打印接口地址:"+postURL+"报文:"+ postXml);
				
						result = Util.doHttpPostUseHead(postURL,postXml,charset,timeout,enableSSLflag,headMap);							 
						//log.debug("打印调用保单打印接口结果:"+result);
								
						JSONObject obj = JSONObject.fromObject(result);
						//log.debug("转JSON对象打印调用保单打印接口结果:"+result);
								
						succ=obj.get("succ");
						log.debug("成功标识，请求失败会返回false:"+succ);
					if(succ==true)
					{
						obj= obj.get("data");
						//log.debug("接口data数据:"+obj);
						reCompulsoryPrintDate=obj.get("compulsory_printDate");
						//log.debug("交强险保单打印时间:"+reCompulsoryPrintDate);
						reBusinessPrintDate=obj.get("business_printDate");
						//log.debug("交强险保单打印时间:"+reBusinessPrintDate);
					
						//log.debug("准备处理续保短信校验接口结果SQL");
						def selectSQL3="SELECT COUNT(*) as COUNT FROM printedchart WHERE POLICYNO='"+policyNo+"'";
						def sqlResult=eliteBean.chaxun_sql(selectSQL3, "dataSource");
						def Countnum =sqlResult.get(0).get("COUNT");
						//log.debug("查询重复结果:"+Countnum);
						if(Countnum>0)
						{
							def updateSQL1="UPDATE printedchart SET COMPULSORY_PRINTDATE=(select case when '"+reCompulsoryPrintDate+"'!='null' then '"+reCompulsoryPrintDate+"' else '' end from dual),BUSINESS_PRINTDATE=(select case when '"+reBusinessPrintDate+"'!='null' then '"+reBusinessPrintDate+"' else '' end from dual),MODIFEDDATE=sysdate  WHERE POLICYNO='"+policyNo+"'";
							//log.debug("更新打印信息:"+updateSQL1);
							eliteBean.update(updateSQL1,'dataSource');
							//log.debug("修改保单打印成功");
						}
						else
						{			
							def insertSQL = "insert into printedchart (POLICYNO,COMPULSORY_POLICYNO,BUSINESS_POLICYNO,PROPOSALNO,RISKCODE,COMPULSORY_PRINTDATE,BUSINESS_PRINTDATE,NEW_OLD_STATUS,CREATEDDATE)select '"+policyNo+"','"+compulsoryPolicyNo+"','"+businessPolicyNo+"','"+proposalNo+"','"+riskCode+"',case when '"+reCompulsoryPrintDate+"'!='null' then '"+reCompulsoryPrintDate+"'else '' end,case when '"+reBusinessPrintDate+"'!='null' then '"+reBusinessPrintDate+"' else '' end,'"+QT_ID+"',sysdate from dual";
							//log.debug("打印新增信息" + insertSQL);
							eliteBean.update(insertSQL, 'dataSource');
							//log.debug("新增保单打印成功");
						}
												
					}
				else{
						ErrType=obj.get("errType");
						ErrMsg=obj.get("errMsg");
						//log.debug("返回错误信息描述:"+ErrMsg);
					}
								
				}
				log.debug("调用保单打印标识接口成功!");
		}
	else
		{
				log.debug("调用保单打印标识接口失败");
		}		

	}
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		return Rev_Value;
	}
}